package cn.ulc.socket.admin.service.goods.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.model.param.SortParam;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.goods.GoodsDTO;
import cn.ulc.socket.admin.model.dto.goods.GoodsListDTO;
import cn.ulc.socket.admin.model.param.goods.GoodsSaveParam;
import cn.ulc.socket.admin.service.goods.GoodsServ;
import cn.ulc.store.dao.goods.GoodsDao;
import cn.ulc.store.dao.goods.GoodsSpecDao;
import cn.ulc.store.entity.goods.Goods;
import cn.ulc.store.entity.goods.GoodsSpec;
import cn.ulc.store.entity.staff.Staff;
import lombok.AllArgsConstructor;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class GoodsServImpl extends BaseService implements GoodsServ {

    GoodsDao goodsDao;
    GoodsSpecDao goodsSpecDao;

    @Override
    public GoodsListDTO findWaterListPage(String name, Long goodsTypeId, PaginationParam pager) {
        Staff loginUser = getCurrentUser();
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",name);
        condition.put("goodsTypeId",goodsTypeId);
        condition.put("companyId",loginUser.getCompanyId());
        condition.put("goodsType", PubDataConstant.TYPE_DEPOSIT);
        return findListPage(condition,pager);
    }

    @Override
    public GoodsListDTO findListPage(String name, Long goodsTypeId, PaginationParam pager) {
        Staff loginUser = getCurrentUser();
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",name);
        condition.put("goodsTypeId",goodsTypeId);
        condition.put("companyId",loginUser.getCompanyId());
        return findListPage(condition,pager);
    }

    public GoodsListDTO findListPage(Map<String,Object> condition, PaginationParam pager) {
        SortParam sortParam = new SortParam("createAt",SortParam.DESC);
        PageQuery pageQuery = initPageQuery(pager,sortParam);
        pageQuery.setParas(condition);
        goodsDao.findListPage(pageQuery);
        return new GoodsListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public GoodsDTO findDetail(Long id) {
        Staff loginUser = getCurrentUser();
        Goods goods =  goodsDao.findById(id);
        if(goods == null) throw new PlatformException("获取商品信息失败");
        List<GoodsSpec> specList = goodsSpecDao.findByGoodsId(loginUser.getCompanyId(),goods.getId());
        return new GoodsDTO(goods,specList);
    }

    @Override
    @Transactional
    public Boolean save(GoodsSaveParam param) {
        Staff loginUser = getCurrentUser();
        List<GoodsSpec> specList = param.getSpecList();
        if(specList == null || specList.size() == 0) throw new PlatformException("商品至少存在一条规格");
        Goods checkBarCode = goodsDao.findByBarCode(loginUser.getCompanyId(),param.getBarCode());
        if(checkBarCode!=null) throw new PlatformException("商品编码重复");
        Goods goods = new Goods();
        boolean isEdit = param.getId() != null  && param.getId() > 0;
        if(isEdit){
            goods = goodsDao.findOne(param.getId());
            if(goods == null) throw new PlatformException("未获取到商品信息");
        }
        else{
            goods.setBarCode(param.getBarCode());
            goods.setStatus(AvailableStatus.AVAILABLE);
        }
        setBaseInfo(goods);
        goods.setName(param.getName());
        if(StrUtil.isBlank(param.getMainImage())){
            goods.setMainImage(specList.get(0).getMainImage());
        }else {
            goods.setMainImage(param.getMainImage());
        }
        goods.setGoodsTypeId(param.getGoodsTypeId());
        goods.setUnitId(param.getUnitId());
        goods.setContent(param.getContent());
        goodsDao.save(goods);

        int sort = 0;
        for (GoodsSpec spec : specList) {
            setBaseInfo(spec);
            if(spec.getDeleted() != DeletedStatus.DELETED) {
                sort++;
                spec.setSort(sort);
            }
            spec.setGoodsId(goods.getId());
        }
        goodsSpecDao.saveAll(specList);
        return true;
    }

    @Override
    @Transactional
    public Boolean deleted(Long id) {
        Staff loginUser = getCurrentUser();
        Goods goods = goodsDao.findOne(id);
        if(goods == null) throw new PlatformException("获取商品信息失败");
        List<GoodsSpec> specList = goodsSpecDao.findByGoodsId(loginUser.getCompanyId(),goods.getId());
        if(specList.size()>0){
            for(GoodsSpec spec:specList){
                setBaseInfo(spec);
                spec.setDeleted(DeletedStatus.DELETED);
            }
            goodsSpecDao.saveAll(specList);
        }
        goods.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(goods);
        goodsDao.save(goods);
        return true;
    }

    @Override
    public Boolean changeStatus(Long id, AvailableStatus status) {
        long gid = Convert.toLong(id,0l);
        Goods goods = goodsDao.findOne(gid);
        if(goods == null) throw new PlatformException("获取商品信息失败");
        setBaseInfo(goods);
        goods.setStatus(status);
        goodsDao.save(goods);
        return true;
    }

}
